package main.view;

import main.model.Video;

import javax.swing.*;
import java.awt.*;
import java.util.List;

import javafx.embed.swing.JFXPanel;
import javafx.application.Platform;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import java.awt.event.ActionListener;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import java.io.File;

public class FrontView extends JFrame {
    private JList<Video> videoList;
    private DefaultListModel<Video> listModel;
    private JButton playButton;
    private JPanel videoPanel;

    public FrontView() {
        setTitle("在线视频播放器");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 视频列表
        listModel = new DefaultListModel<>();
        videoList = new JList<>(listModel);
        videoList.setCellRenderer(new VideoListRenderer());
        add(new JScrollPane(videoList), BorderLayout.WEST);

        // 播放按钮
        playButton = new JButton("播放");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(playButton);
        add(buttonPanel, BorderLayout.SOUTH);

        // 视频播放面板
        videoPanel = new JPanel();
        add(videoPanel, BorderLayout.CENTER);
    }

    public void setVideoList(List<Video> videos) {
        listModel.clear();
        for (Video video : videos) {
            listModel.addElement(video);
        }
    }

    public Video getSelectedVideo() {
        return videoList.getSelectedValue();
    }

    public void addPlayButtonListener(ActionListener listener) {
        playButton.addActionListener(listener);
    }

    public void playVideo(String filePath) {
        videoPanel.removeAll();

        // 使用JavaFX嵌入播放器（需要JDK中包含JavaFX）
        try {
            JFXPanel fxPanel = new JFXPanel();
            videoPanel.add(fxPanel, BorderLayout.CENTER);

            Platform.runLater(() -> {
                Media media = new Media(new File(filePath).toURI().toString());
                MediaPlayer player = new MediaPlayer(media);
                MediaView view = new MediaView(player);

                StackPane root = new StackPane();
                root.getChildren().add(view);
                Scene scene = new Scene(root);
                fxPanel.setScene(scene);

                player.play();
            });
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "无法播放视频: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }

        videoPanel.revalidate();
        videoPanel.repaint();
    }

    // 自定义列表渲染器
    private class VideoListRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                                                      boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Video) {
                Video video = (Video) value;
                setText(video.getTitle() + " (" + video.getViewCount() + "次播放)");
            }
            return this;
        }
    }
}
